package com.chenyi.web.controller.shop;

import com.chenyi.common.annotation.UserRequired;
import com.chenyi.common.constant.Constants;
import com.chenyi.common.core.controller.BaseController;
import com.chenyi.common.core.domain.entity.SysShop;
import com.chenyi.common.core.domain.entity.SysShopMenu;
import com.chenyi.common.core.domain.model.LoginBody;
import com.chenyi.common.core.domain.model.LoginShop;
import com.chenyi.common.utils.ServletUtils;
import com.chenyi.common.utils.StringUtils;
import com.chenyi.framework.manager.AsyncManager;
import com.chenyi.framework.manager.factory.AsyncFactory;
import com.chenyi.framework.web.service.ShopLoginService;
import com.chenyi.framework.web.service.ShopTokenService;
import com.chenyi.shop.service.ISysShopMenuService;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 登录验证
 *
 * @author wangkh
 */
@RestController
@RequestMapping("/shop")
public class ShopLoginController  extends BaseController {

    @Autowired
    private ShopLoginService loginService;
    @Autowired
    private ISysShopMenuService menuService;
    @Autowired
    private ShopTokenService tokenService;

    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    public ResponseEntity<Object> login(@RequestBody LoginBody loginBody)
    {
        Map<String,Object> data = Maps.newHashMap();
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid());
        data.put(Constants.TOKEN, token);
        return ResponseEntity.ok(data);
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @UserRequired
    @GetMapping("/getInfo")
    public ResponseEntity<Object> getInfo()
    {
        LoginShop loginShop = tokenService.getLoginShop(ServletUtils.getRequest());
        SysShop shop = loginShop.getShop();
        // 角色集合
        Map<String,Object> data = Maps.newHashMap();
        data.put("shop", shop);
        return ResponseEntity.ok(data);
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @UserRequired
    @GetMapping("/getRouters")
    public ResponseEntity<Object> getRouters()
    {
        LoginShop loginShop = tokenService.getLoginShop(ServletUtils.getRequest());
        // 用户信息
        SysShop shop = loginShop.getShop();
        List<SysShopMenu> menus = menuService.selectShopMenuTreeByShopId(shop.getShopId());
        return ResponseEntity.ok(menuService.buildShopMenus(menus));
    }
    /**
     * 退出登录
     *
     * @return 路由信息
     */
    @UserRequired
    @PostMapping("/logout")
    public ResponseEntity<Object> logout(HttpServletRequest request){
        LoginShop loginShop = tokenService.getLoginShop(request);
        if (StringUtils.isNotNull(loginShop))
        {
            String userName = loginShop.getShop().getUserName();
            // 删除用户缓存记录
            tokenService.delLoginShop(loginShop.getToken());
            // 记录用户退出日志
            AsyncManager.me().execute(AsyncFactory.recordShopLogininfor(userName, Constants.LOGOUT, "退出成功",loginShop.getToken()));
        }
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
